package com.jinmdz.fmis.dao.dao.provider;

import com.jinmdz.fmis.core.constant.EDictCode;
import com.jinmdz.fmis.dao.base.BaseProvider;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.util.StringUtils;

import java.util.ArrayList;

/**
 * 费用结算Provider
 *
 * @author LiCongLu
 * @date 2020-02-21 14:50
 */
public class ChargeSettleProvider extends BaseProvider {

    /**
     * 按照主键获取费用结算信息
     *
     * @param id 主键
     * @return
     * @author LiCongLu
     * @date 2020-02-21 15:36
     */
    public String getChargeSettleViewById(@Param("id") Integer id) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" charge_settle.id,charge_settle.business_code,settle_batch_no,receipt_charge,discount_charge,benefit_charge");
                builder.append(" ,settle_charge,settle_time,settle_type_code,receipt_no,as_refund,as_verify,verify_id,as_invoice,invoice_id");
                builder.append(" ,settle_user_id,charge_settle.random_code,charge_settle.remark,charge_settle.version");
                builder.append(" ,system_user.full_name as settle_user_name");

                // 关联查询付款方式
                builder.append(" ,( select group_concat(distinct data_dictionary.data_text) from charge_settle_pay ");
                builder.append("   left join data_dictionary ON charge_settle_pay.pay_type_code = data_dictionary.data_code and data_dictionary.dict_code='" + EDictCode.PAY_TYPE.getCode() + "'");
                builder.append("   where charge_settle_pay.charge_settle_id = charge_settle.id");
                builder.append("  ) as pay_type ");
                SELECT(builder.toString());
                FROM(" charge_settle ");
                LEFT_OUTER_JOIN(" system_user on system_user.id = charge_settle.settle_user_id  ");
                WHERE(" charge_settle.deleted = 0 ");
                WHERE(" charge_settle.id = #{id} ");
                ORDER_BY(" charge_settle.id ");
            }
        }.toString();
    }

    /**
     * 按照业务编码查询费用结算信息
     *
     * @param businessCode     业务编码
     * @param invoiceStateCode 作废发票编码
     * @return
     * @author LiCongLu
     * @date 2020-02-21 14:52
     */
    public String listChargeSettleViewByBusinessCode(@Param("businessCode") String businessCode, @Param("invoiceStateCode") Integer invoiceStateCode) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" charge_settle.id,charge_settle.business_code,settle_batch_no,receipt_charge,discount_charge,benefit_charge");
                builder.append(" ,settle_charge,settle_time,settle_type_code,receipt_no,as_refund,as_verify,verify_id,as_invoice,invoice_id");
                builder.append(" ,settle_user_id,charge_settle.random_code,charge_settle.remark,charge_settle.version");
                builder.append(" ,system_user.full_name as settle_user_name");
                builder.append(" ,invoice_code,invoice_no,invoice_title");

                // 关联查询付款方式
                builder.append(" ,( select group_concat(distinct data_dictionary.data_text) from charge_settle_pay ");
                builder.append("   left join data_dictionary ON charge_settle_pay.pay_type_code = data_dictionary.data_code and data_dictionary.dict_code='" + EDictCode.PAY_TYPE.getCode() + "'");
                builder.append("   where charge_settle_pay.charge_settle_id = charge_settle.id");
                builder.append("  ) as pay_type ");
                SELECT(builder.toString());
                FROM(" charge_settle ");
                LEFT_OUTER_JOIN(" system_user on system_user.id = charge_settle.settle_user_id  ");
                LEFT_OUTER_JOIN(" charge_invoice on charge_invoice.id = invoice_id and invoice_state_code != #{invoiceStateCode} ");
                WHERE(" charge_settle.deleted = 0 ");
                WHERE(" charge_settle.business_code = #{businessCode} ");
                ORDER_BY(" charge_settle.id ");
            }
        }.toString();
    }

    /**
     * 按照主键集合获取费用结算信息
     *
     * @param businessCode 业务编码
     * @param settleIds    结算费用ID集合
     * @return
     * @author ZhangLiRui
     * @date 2020/4/16 13:55
     */
    public String getPayTypeBySettleIds(@Param("businessCode") String businessCode, @Param("settleIds") ArrayList<Integer> settleIds) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append("  group_concat(distinct data_dictionary.data_text) as pay_type  ");
                SELECT(builder.toString());
                FROM(" charge_settle_pay ");
                JOIN(" data_dictionary on charge_settle_pay.pay_type_code = data_dictionary.data_code and data_dictionary.dict_code='" + EDictCode.PAY_TYPE.getCode() + "'");
                WHERE(" charge_settle_pay.deleted = 0 ");
                WHERE(" charge_settle_pay.business_code = #{businessCode} ");
                // 主键
                String idString = StringUtils.arrayToDelimitedString(settleIds.toArray(new Integer[]{}), ",");
                WHERE(" charge_settle_pay.charge_settle_id in (" + idString + ")");
            }
        }.toString();
    }

    /**
     * 设置结算打印发票
     *
     * @param businessCode   业务编码
     * @param settleIds      结算集合
     * @param invoiceId      发票主键
     * @param modifiedUserId 修改人
     * @return
     * @author LiCongLu
     * @date 2020-05-14 09:13
     */
    public String updateForInvoice(@Param("businessCode") String businessCode, @Param("settleIds") ArrayList<Integer> settleIds, @Param("invoiceId") Integer invoiceId, @Param("modifiedUserId") Integer modifiedUserId) {
        return new SQL() {
            {
                UPDATE(" charge_settle ");
                SET(" as_invoice = 1 ");
                SET(" invoice_id = #{invoiceId} ");
                SET(" modified_user_id = #{modifiedUserId} ");
                SET(" version = unix_timestamp(now()) ");
                WHERE(" deleted = 0 ");
                WHERE(" business_code = #{businessCode} ");

                // 主键
                String idString = StringUtils.arrayToDelimitedString(settleIds.toArray(new Integer[]{}), ",");
                WHERE(" id in (" + idString + ")");
            }
        }.toString();
    }

    /**
     * 设置作废发票
     *
     * @param businessCode   业务编码
     * @param invoiceId      发票主键
     * @param modifiedUserId 修改人
     * @return
     * @author LiCongLu
     * @date 2020-05-28 16:10
     */
    public String updateForInvoiceCancellation(@Param("businessCode") String businessCode, @Param("invoiceId") Integer invoiceId, @Param("modifiedUserId") Integer modifiedUserId) {
        return new SQL() {
            {
                UPDATE(" charge_settle ");
                SET(" as_invoice = 0 ");
                SET(" invoice_id = null ");
                SET(" modified_user_id = #{modifiedUserId} ");
                SET(" version = unix_timestamp(now()) ");
                WHERE(" deleted = 0 ");
                WHERE(" business_code = #{businessCode} ");
                WHERE(" invoice_id = #{invoiceId} ");
            }
        }.toString();
    }

    /**
     * 按照结算主键批量获取结算列表
     *
     * @param businessCode 业务编码
     * @param settleIds    结算主键集合
     * @return
     * @author LiCongLu
     * @date 2020-05-14 09:43
     */
    public String listChargeSettleViewBySettleIds(@Param("businessCode") String businessCode, @Param("settleIds") ArrayList<Integer> settleIds) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" charge_settle.id,charge_settle.business_code,settle_batch_no,receipt_charge,discount_charge,benefit_charge");
                builder.append(" ,settle_charge,settle_time,settle_type_code,receipt_no,as_refund,as_verify,verify_id,as_invoice,invoice_id");
                builder.append(" ,settle_user_id,charge_settle.random_code,charge_settle.remark,charge_settle.version");
                builder.append(" ,system_user.full_name as settle_user_name");

                // 关联查询付款方式
                builder.append(" ,( select group_concat(distinct data_dictionary.data_text) from charge_settle_pay ");
                builder.append("   left join data_dictionary ON charge_settle_pay.pay_type_code = data_dictionary.data_code and data_dictionary.dict_code='" + EDictCode.PAY_TYPE.getCode() + "'");
                builder.append("   where charge_settle_pay.charge_settle_id = charge_settle.id");
                builder.append("  ) as pay_type ");
                SELECT(builder.toString());
                FROM(" charge_settle ");
                LEFT_OUTER_JOIN(" system_user on system_user.id = charge_settle.settle_user_id  ");
                WHERE(" charge_settle.deleted = 0 ");
                WHERE(" charge_settle.business_code = #{businessCode} ");
                // 主键
                String idString = StringUtils.arrayToDelimitedString(settleIds.toArray(new Integer[]{}), ",");
                WHERE(" charge_settle.id in (" + idString + ")");
                ORDER_BY(" charge_settle.id ");
            }
        }.toString();
    }
}
